package swu.cx.androidmvc

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import swu.cx.androidmvc.Model.Book
import swu.cx.androidmvc.Utils.BookProviderFactory
import swu.cx.androidmvc.ui.IBookView

class MainActivity : AppCompatActivity(),IBookView {
    val presenter = BookPresenter().apply {
            bookView = this@MainActivity
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        submit.setOnClickListener {
            presenter.checkInput(bookName.text.toString(),athorName.text.toString())
        }
    }

    override fun inputIsValid(valid: Boolean) {
        if(!valid){
            Toast.makeText(this,"输入不合法！！",Toast.LENGTH_SHORT).show()
        }else{
            val book = Book(bookName.text.toString(),athorName.text.toString())
            presenter.addBook(book)
            bookName.setText("")
            athorName.setText("")
        }
    }

    override fun showBooks(book: List<Book>) {
        val builder = StringBuilder()
        book.forEach {book ->
            builder.append("${book.name}-${book.author} \n")
        }
        content.text = builder.toString()
    }
}